package cn.tinlan.shopcoin.Utils;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import cn.tinlan.shopcoin.app.login.UserModel;

/**
 * Created by Mortimer on 2018/12/11. 15:50
 * 佛祖🙏，永无bug。
 */
public class CacheManager {

    //保存文件的路径
    static String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/people.txt";

    /**
     * 获取user对象
     */
    public static UserModel readUserModel(Context context){
        return (UserModel)readObject(context,path);
    }

    /**
     * 保存user对象
     */
    public static boolean saveUserModel(Context context,Serializable ser){
        return saveObject(context,ser,path);
    }

    /**
     * 保存对象
     *
     * @param ser 要保存的序列化对象
     * @param file 保存在本地的文件名
     *
     */
    public static boolean saveObject(Context context, Serializable ser,
                                     String file) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = context.openFileOutput(file, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(ser);
            oos.flush();
            return true;
        } catch ( Exception e ) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                oos.close();
            } catch ( Exception e ) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 读取对象
     *
     * @param file 保存在本地的文件名
     * @return
     *
     */
    public static Serializable readObject(Context context, String file) {
        if ( !SPUtils.contains(context, file) )
            return null;
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = context.openFileInput(file);
            ois = new ObjectInputStream(fis);
            return (Serializable) ois.readObject();
        } catch ( FileNotFoundException e ) {
        } catch ( Exception e ) {
            e.printStackTrace();
            // 反序列化失败 - 删除缓存文件
            if ( e instanceof InvalidClassException) {
                File data = context.getFileStreamPath(file);
                data.delete();
            }
        } finally {
            try {
                ois.close();
            } catch ( Exception e ) {
            }
            try {
                fis.close();
            } catch ( Exception e ) {
            }
        }
        return null;
    }


}
